package com.ruoyi.ordersys.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.ordersys.domain.OrderDetails;
import com.ruoyi.ordersys.mapper.OrdersMapper;
import com.ruoyi.ordersys.domain.Orders;
import com.ruoyi.ordersys.service.IOrdersService;

/**
 * 订单Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-07-30
 */
@Service
public class OrdersServiceImpl implements IOrdersService 
{
    @Autowired
    private OrdersMapper ordersMapper;

    /**
     * 查询订单
     * 
     * @param orderId 订单主键
     * @return 订单
     */
    @Override
    public Orders selectOrdersByOrderId(Long orderId)
    {
        return ordersMapper.selectOrdersByOrderId(orderId);
    }

    /**
     * 查询订单列表
     * 
     * @param orders 订单
     * @return 订单
     */
    @Override
    public List<Orders> selectOrdersList(Orders orders)
    {
        return ordersMapper.selectOrdersList(orders);
    }

    /**
     * 新增订单
     * 
     * @param orders 订单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertOrders(Orders orders)
    {
        int rows = ordersMapper.insertOrders(orders);
        insertOrderDetails(orders);
        return rows;
    }

    /**
     * 修改订单
     * 
     * @param orders 订单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateOrders(Orders orders)
    {
        ordersMapper.deleteOrderDetailsByOrderNum(orders.getOrderId());
        insertOrderDetails(orders);
        return ordersMapper.updateOrders(orders);
    }

    /**
     * 批量删除订单
     * 
     * @param orderIds 需要删除的订单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOrdersByOrderIds(Long[] orderIds)
    {
        ordersMapper.deleteOrderDetailsByOrderNums(orderIds);
        return ordersMapper.deleteOrdersByOrderIds(orderIds);
    }

    /**
     * 删除订单信息
     * 
     * @param orderId 订单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOrdersByOrderId(Long orderId)
    {
        ordersMapper.deleteOrderDetailsByOrderNum(orderId);
        return ordersMapper.deleteOrdersByOrderId(orderId);
    }

    /**
     * 新增订单详情信息
     * 
     * @param orders 订单对象
     */
    public void insertOrderDetails(Orders orders)
    {
        List<OrderDetails> orderDetailsList = orders.getOrderDetailsList();
        Long orderId = orders.getOrderId();
        if (StringUtils.isNotNull(orderDetailsList))
        {
            List<OrderDetails> list = new ArrayList<OrderDetails>();
            for (OrderDetails orderDetails : orderDetailsList)
            {
                orderDetails.setOrderNum(orderId.toString());
                list.add(orderDetails);
            }
            if (list.size() > 0)
            {
                ordersMapper.batchOrderDetails(list);
            }
        }
    }
}
